<%@page import="Model.Mobile"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="Model.ConnectDatabase"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Admin</title>
        <link href="960.css" rel="stylesheet" type="text/css" media="all" />
        <link href="text.css" rel="stylesheet" type="text/css" media="all" />
        <link href="style.css" rel="stylesheet" type="text/css" media="all" />

        <%
                    String manufacturer_id = request.getParameter("manufacturer");
                    String keyword = request.getParameter("keyword");
                    manufacturer_id = (manufacturer_id == null) ? "" : manufacturer_id;
                    keyword = (keyword == null) ? "" : keyword;

                    int numOfPage = 1;
                    try {
                        numOfPage = (Integer) request.getAttribute("numOfPage");
                    } catch (Exception ex) {
                        int numOfRecord = Mobile.countRecord("", "");
                        numOfPage = (numOfRecord % 20 == 0) ? numOfRecord / 20 : numOfRecord / 20 + 1;
                    }

                    ArrayList<Mobile> listMobile = (ArrayList<Mobile>) request.getAttribute("listResult");
                    int listSize = 0;
                    if (listMobile == null) {
                        listMobile = Mobile.searchMobile("", "", 1);
                        listSize = listMobile.size();
                    } else {
                        listSize = listMobile.size();
                    }

        %>

        <script type="text/javascript">
            function areYouSure(){
                return confirm("Are you sure?");
                
            }
        </script>

    </head>

    <body>
        <div class="container_16" id="content">
            <div class="" id="header">
                <div class="grid_4 alpha" id="admin"> </div>
                <div class="grid_12 omega" id="menu">
                    <jsp:include page="Navigation.jsp" flush="true">
                        <jsp:param name="productManage" value="current"/>
                    </jsp:include>
                </div> <!-- End #menu -->
                <a style="color: red" href="../logout">Thoát</a>
            </div> <!-- End #header -->


            <div class="grid_16 alpha omega" id="mainContent">
                <div class="grid_4 alpha" id="leftPanel">

                    <h2>Tìm kiếm</h2>
                    <form action="MobileSearch?page=1">
                        <table>
                            <tr>
                                <td>
                                    Tên điện thoại:
                                </td>
                                <td >
                                    <input type="text" name="keyword" size="15"><br/>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Nhà sản xuất:
                                </td>
                                <td>
                                    <select size="1" name="manufacturer">
                                        <option value=""></option>
                                        <%
                                                    Connection con1 = ConnectDatabase.getConnection();
                                                    Statement sta1 = con1.createStatement();
                                                    String sql1 = "SELECT * FROM manufacturer";
                                                    ResultSet re1 = sta1.executeQuery(sql1);
                                                    while (re1.next()) {
                                        %>
                                        <option value="<%=re1.getInt(1)%>"><%=re1.getString(2)%></option>
                                        <%}%>
                                    </select>
                                </td>
                            </tr>
                        </table>
                        <input type="submit" value="Tìm">
                    </form>
                    <br/>
                    <div align="center" style="margin-top: 50px;">
                        <a href="AddProduct.jsp" id="addNew"></a> <br/>
                        <h3 style="margin-top: 0px;">Thêm sản phẩm</h3>
                    </div>
                </div> <!-- End #leftPanel -->

                <div class="grid_12 omega" id="mainPanel">
                    <table cellspacing="5">
                        <tr>
                            <th></th>
                            <th width="30%"><h3>Tên sản phẩm</h3></th>
                            <th width="20%"><h3>Giá</h3></th>
                            <th><h3>SL Tồn kho</h3></th>
                        </tr>
                        <%
                                    for (int i = 0; i < listSize; i++) {
                                        Mobile mobile = listMobile.get(i);
                        %>
                        <tr>
                            <td> <img src="../<%=mobile.getAnhDaiDien()%>" alt="" width="60"></td>
                            <td valign="middle" align="center"><%=mobile.getTenSP()%></td>
                            <td valign="middle" align="center"><%=mobile.getGiaBan()%></td>
                            <td valign="middle" align="center"><%=mobile.getSoLuong()%></td>
                            <td valign="middle" align="center">
                                <form action="DeleteProduct">
                                    <input type="submit" value="Xóa" onclick="return areYouSure()">
                                    <input type="hidden" value="<%=mobile.getMobileID()%>" name="mobileID">
                                </form>
                            </td>
                            <td valign="middle" align="center">
                                <form action="InfoProduct" method="get">
                                    <input type="submit" value="Cập nhật">
                                    <input type="hidden" value="<%=mobile.getMobileID()%>" name="mobileID">
                                </form>
                            </td>
                        </tr>
                        <%}%>
                    </table>

                    <%
                                for (int i = 1; i <= numOfPage; i++) {%>
                    |<a  href="MobileSearch?keyword=<%=keyword%>&manufacturer=<%=manufacturer_id%>&page=<%=i%>"> <%= i%> </a>|
                    <%}%>


                </div> <!-- End #mainPanel -->
            </div> <!-- End #mainContent -->

            <div id="footer" align="center"><font color="white">© 2011 Shop CH4</font></div>
        </div>
    </body>


</html>
